package com.cloudoer.blog.controller;

import com.cloudoer.blog.base.BaseController;
import com.cloudoer.blog.model.Member;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/member")
public class MemberController extends BaseController {

    @RequestMapping(value = "/index")
    public String index (){
        return "member/index";
    }

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public String info (String id, ModelMap modelMap){
        modelMap.put("member", memberService.getById(id));
        return "member/info";
    }

    @RequestMapping(value = "/getByPage")
    @ResponseBody
    public Map getByPage (@RequestParam(value = "pno", defaultValue = "1") Integer pno, @RequestParam(value = "psize", defaultValue = "10") Integer psize){
        List<Member> list = memberService.getByPage((pno-1)*psize, psize);
        return result(list, memberService.getCount(), "");
    }

    @RequestMapping(value = "/getById", method = RequestMethod.POST)
    @ResponseBody
    public Member getById (String id){
        return memberService.getById(id);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map delete (String id){
        memberService.delete(id);
        return result(SUCCESS, "操作成功");
    }
}
